var express = require('express');
var router = express.Router();
const { UsersModel,OrderModel,commodityModel } = require('../../model/model')
/* GET home page. */

//获取用户信息
router.get('/get_suer_list', async (req, res) => {
  let { currentPage, pageSize } = req.query;
  let list = await UsersModel.find().limit(pageSize).skip((currentPage - 1) * pageSize)
  let total = await UsersModel.find().countDocuments()
  res.send({
    code: 200,
    list,
    total
  })
});

// 添加优惠券
router.post('/add_discount', async (req, res) => {
  let { _id, obj1, obj2, status } = req.body;
  let box = await UsersModel.find({ _id })
  if (status == 1) {
    box[0].my_coupon.push(obj1)
    await UsersModel.updateOne({ _id }, box[0])
  } else if (status == 2) {
    box[0].my_gladpage.push(obj2)
    await UsersModel.updateOne({ _id }, box[0])
  } else if (status == 3) {
    box[0].my_coupon.push(obj3)
    box[0].my_gladpage.push(obj2)
    await UsersModel.updateOne({ _id }, box[0])
  }
})

//添加金叶子
router.post('/add_discounts', async (req, res) => {
  let { ids, obj1, obj2, status } = req.body;
  let box = await UsersModel.find()
  if (status == 1) {
    box.forEach(async item => {
      ids.forEach(async id => {
        if (item._id == id) {
          console.log(item);
          item.my_coupon.push(obj1)
          await UsersModel.updateOne({ _id: item._id }, item)
        }
      })
    })
  } else if (status == 2) {
    box.forEach(async item => {
      ids.forEach(async id => {
        if (item._id == id) {
          item.my_gladpage.push(obj2)
          await UsersModel.updateOne({ _id: item._id }, item)
        }
      })
    })
  } else if (status == 3) {
    box.forEach(async item => {
      ids.forEach(async id => {
        if (item._id == id) {
          item.my_coupon.push(obj1)
          item.my_gladpage.push(obj2)
          await UsersModel.updateOne({ _id: item._id }, item)
        }
      })
    })
  }
})

//获取订单列表
router.post('/get_order_list', async (req, res) => {
  let {username,names,address,tel,currentPage, pageSize,type,deliver,withdraw} = req.body
  console.log(username,names,address,tel);
  let looklist=[]
  let username1=RegExp(username)
  let names1=RegExp(names)
  let address1=RegExp(address)
  if(tel) looklist.push({tel:tel})
  if(username) looklist.push({username:username1})
  if(names) looklist.push({names:names1})
  if(address) looklist.push({address:address1})
  if(looklist.length==0) looklist.push({})
  if(type) looklist.push({parment: type})
  if(deliver) looklist.push({deliver: deliver})
  if(withdraw) looklist.push({withdraw: withdraw})
  console.log(looklist);
  let list=await OrderModel.find({$and:looklist}).limit(pageSize).skip((currentPage - 1) * pageSize)
  let total = await UsersModel.find().countDocuments()
  res.send({
    code: 200,
    list,
    total
  })
})


//获取商品信息
router.get('/get_product', async (req, res) => {
  let list = await commodityModel.find()
  res.send({
    code: 200,
    list
  })
})
//删除订单
router.post('/del_order', async (req, res) => {
  let { _id } = req.body
  await OrderModel.deleteOne({ _id: _id })
  res.send({
    code: 200,
    msg: '删除成功'
  })
})

module.exports = router;
